package com.weigee.ktv.p.frag;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.weigee.ktv.m.retrofit.ApiRequest;
import com.weigee.ktv.p.TaskDetailContract;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by linde on 2017/5/5.
 * <p/>
 */
public class FirstContentPresenterImpl implements TaskDetailContract.FirstContentPresenter, Callback<ResponseBody>
{
    private final TaskDetailContract.FirstContentView view;

    public FirstContentPresenterImpl(@NonNull TaskDetailContract.FirstContentView view)
    {
        this.view = view;
    }

    @Override
    public void onCreated(Bundle savedInstanceState)
    {
    }

    @Override
    public void onViewCreated(Bundle savedInstanceState)
    {
        onLoadListData(false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void onLoadData(Object... objects)
    {

    }

    @Override
    public void onLoadListData(boolean moreData)
    {
        ApiRequest.firstContent(this);
    }

    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
    {
        try
        {
            final String respStr = response.body().string();
            if (!TextUtils.isEmpty(respStr))
            {
                JSONObject object = JSONObject.parseObject(respStr);
                String ivPageTop = object.getString("ivPageTop");
                String ivRoomReservation = object.getString("ivRoomReservation");
                String ivRequestSong = object.getString("ivRequestSong");
                String ivSupermarket = object.getString("ivSupermarket");

                view.loadImage(ivPageTop, ivRoomReservation, ivRequestSong, ivSupermarket);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t)
    {
        t.printStackTrace();
    }
}
